//
//  SinaOauthViewController.swift
//  weibo
//
//  Created by guyue on 16/1/2.
//  Copyright © 2016年 guyue. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking


class SinaOauthViewController: UIViewController,UIWebViewDelegate{

    //设置web页面为返回视图
    let webView = UIWebView()
    
    override func loadView() {
        
        
        view = webView
        
        //调用导航页面的设置
        setupNav()
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //页面跳转的时候,要注意View的背景颜色
        view.backgroundColor = UIColor.whiteColor()
        
        //加载授权页面
        
        //URL
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=3289499813&redirect_uri=http://www.baidu.com")
        //创建一个请求, url 可以强制解包,因为这个url是写死的
        
        let request = NSURLRequest(URL: url!)
        
        
        //加载
        webView.loadRequest(request)
        
        //设置代理.拿到url
        webView.delegate = self
        
    }
    
    //MARK:--设置页面导航
    
    private func setupNav(){
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "dismiss")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autofill")
    
    }
    
    //MARK:-页面导航要实现的方法
    func dismiss(){
    
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    
    }
    
    func autofill(){
    
        //js
       let js = "document.getElementById('userId').value = \"549446728@qq.com\";document.getElementById('passwd').value = \"GUYUEXIN\""
        
        //web加载js
        
        webView.stringByEvaluatingJavaScriptFromString(js)
    
        
    }
    
}

//第一步.截取code
  //SinaOauthViewController 的分类,专门写web页面的代理方法
extension SinaOauthViewController {

    //MARK:--web页面的代理方法
    func webViewDidStartLoad(webView: UIWebView) {
        //显示加载的页面
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        //加载消失的页面
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        
        SVProgressHUD.dismiss()
    }
    
    //进行网络请求, 返回true加载页面.返回false,页面不加载的
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        printLog(request.URL?.absoluteString)
        
        //URL是可选项,可能没有值,需要if let判断一下
        if let url = request.URL?.absoluteString {
            
            //string中判断前缀的方法
            if url.hasPrefix(redirect_url) {
                
                // printLog(url)
                //打印出来的url
                //http://www.baidu.com/?code=4b30a163fdfb61d310a39326656d7f4e
                
                //截取code
                
                if let code = request.URL?.query{
                    
                    printLog(code)
                    
                    //打印的code
                    //code=920dd0eebcbaeb1dc3a69fc1a5145dbb
                    
                    //截取code=后面的授权码
                    
                    let subStr = code.substringFromIndex("code=".endIndex)
                    
                    printLog(subStr)
                    
                    //调用发送网络请求的方法
                    
                    
                    //此时getToken这个方法在 SinaAccoountViewModel 里面,这里是访问不到的,要想访问的话,需要创建一个accoountViewModel 实例,而且还是去掉getToken的私有方法,要不然点不出getToken这个方法
                    
                    //let accountViewModel = SinaAccoountViewModel()
                    
                    //用单例来写
                    
                    SinaAccoountViewModel.shareInstance.getToken(subStr, success: { () -> () in
                            printLog("成功了")
                            
                            }, failure: { (error) -> () in
                                printLog(error)
                        })
                    
                  
                    //打印的code=后面的授权码
                    //fd989382f09bd681d1cb2ec4e71a6351
                }
                
                //返回true时,记载百度,返回false时不加载百度,这里只想截取百度网址后面的code而已,所以不需要加载百度
                return false
                
            }
            
            
            
        }
        
        
        return true
    }



}

//第二步.发送网络请求,获取Token
//  获取Token
extension   SinaOauthViewController {

    


}

//第三步,个人信息请求
    extension SinaOauthViewController {
        
}


/*
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("https://api.weibo.com/oauth2/authorize?client_id=3289499813&redirect_uri=http://www.baidu.com")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("https://api.weibo.com/oauth2/authorize")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("https://api.weibo.com/oauth2/authorize#")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("https://api.weibo.com/oauth2/authorize")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("http://www.baidu.com/?code=b7a6dc0da3164a30882f1b51c2f7c803")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("https://m.baidu.com/?code=b7a6dc0da3164a30882f1b51c2f7c803&from=844b&vit=fps")
SinaOauthViewController.swift[103], webView(_:shouldStartLoadWithRequest:navigationType:): Optional("about:blank")

*/
